You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Release discarded SDK response bodies before retrying - #1770
The SDK's fetchWithRetry discards retryable HTTP responses without reading or cancelling their bodies. An unfinished error body can keep a connection occupied while the next request waits for it.
A local HTTP fixture reproduces this through startAgentRun: with a single-connection Undici dispatcher, an unending 503 body prevents the next request from reaching the server. Cancelling that body lets the retry succeed. This follows Undici's response-body ownership guidance.
Changes
Cancel a discarded response body before the existing backoff. Treat cancellation failure as best effort so it cannot replace the retry result. Responses returned to the caller remain readable.
Add one regression through startAgentRun, using real Response/ReadableStream objects and the existing fake retry clock. It checks cleanup order, a failing cancellation, and the eventual successful result.
Verification
The new regression plus the agent-step-batching, fetch-agent-publisher-trust, and delegated-user-header test files: main has 45 passed / 1 failed; with the patch, 46 passed / 0 failed and 1,252 assertions.
Native Node 24.20.0 HTTP probe: main times out with only one request received and the first response still open. The patch closes it and completes the second request.
Local probes confirm HTTP 400 and the final 503 after retry exhaustion still expose their diagnostic body; the request counts stay at one and four respectively.
SDK typecheck passes with the locked native TypeScript 7.0.2 compiler. Frozen-lockfile install passes. ESLint has 0 errors and 4 local resolver warnings. The new test is formatted; the source's existing Prettier differences are unchanged.
Raw SDK and binary builds fail because the locked declaration generator resolves TypeScript 7.0.2, which lacks the ts.sys API it imports. The same SDK failure occurs on main. A supplemental build with a temporary local JavaScript TypeScript mapping reached declaration generation but exhausted Bun memory; the original dependency was restored. No build or smoke pass is claimed.
Notes / Scope
Two SDK files, no dependency or public API changes. Retry statuses, attempt limits, backoff, and request replay policy are unchanged. This patch only releases responses that the retry loop already discards.
Validation used Windows, Bun 1.3.14, and local fixtures without credentials. Other operating systems, production endpoints, and the full monorepo suite were not run. The declaration-build dependency issue remains outside this patch.
Good catch and a minimal, well-targeted fix. In sdk/src/impl/database.ts, the retry path previously discarded the Response from a retryable HTTP error without reading or cancelling its body — under Undici's connection-pooling model this can leave a connection occupied by an abandoned stream, exactly as described in the linked Undici docs. Adding await response.body?.cancel().catch(() => {}) before the existing backoff is the right remedy: it's best-effort (failures don't affect the retry result), it doesn't touch the body of responses actually returned to the caller, and it doesn't change retry policy, status handling, or backoff timing.
The new test in retry-response-body.test.ts is a reasonable regression: it uses real Response/ReadableStream objects, the existing fake retry clock, and asserts both cancellation ordering and that a throwing cancel() doesn't derail the eventual successful result. That's the right level of test for this kind of fix.
A few things worth tightening before porting:
The body text of the diagnostic 503 is still logged via logger?.warn with only status/attempt/url — confirm nothing downstream expects to read the retryable response body for diagnostics after this change (your notes say HTTP 400/exhausted-503 bodies remain readable, but worth double-checking all call sites).
The PR description is very long with build/typecheck environment noise unrelated to the actual change; a maintainer mainly needs the two-file diff and the test rationale.
Overall: narrow, in-scope (sdk/ only), with a clear cause and a matching test — good candidate for porting as-is or with minor logging review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem / Why
The SDK's
fetchWithRetrydiscards retryable HTTP responses without reading or cancelling their bodies. An unfinished error body can keep a connection occupied while the next request waits for it.A local HTTP fixture reproduces this through
startAgentRun: with a single-connection Undici dispatcher, an unending 503 body prevents the next request from reaching the server. Cancelling that body lets the retry succeed. This follows Undici's response-body ownership guidance.Changes
Cancel a discarded response body before the existing backoff. Treat cancellation failure as best effort so it cannot replace the retry result. Responses returned to the caller remain readable.
Add one regression through
startAgentRun, using real Response/ReadableStream objects and the existing fake retry clock. It checks cleanup order, a failing cancellation, and the eventual successful result.Verification
agent-step-batching,fetch-agent-publisher-trust, anddelegated-user-headertest files: main has 45 passed / 1 failed; with the patch, 46 passed / 0 failed and 1,252 assertions.ts.sysAPI it imports. The same SDK failure occurs on main. A supplemental build with a temporary local JavaScript TypeScript mapping reached declaration generation but exhausted Bun memory; the original dependency was restored. No build or smoke pass is claimed.Notes / Scope
Two SDK files, no dependency or public API changes. Retry statuses, attempt limits, backoff, and request replay policy are unchanged. This patch only releases responses that the retry loop already discards.
Validation used Windows, Bun 1.3.14, and local fixtures without credentials. Other operating systems, production endpoints, and the full monorepo suite were not run. The declaration-build dependency issue remains outside this patch.